package service

import (
	"api/internal/svc"
	"context"
	"encoding/json"
	"strconv"
)

type NotifyService struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}
type Notify struct {
	Love bool `json:"love"` // false无效(用户已点击) true 有效(消息通知用户未点击)
	Mine bool `json:"mine"`
	Msg  bool `json:"msg"`
}

type Love struct {
	Cur int `json:"cur"` // cur:当前数,new:新增数 用户点击时设置新增数为0
	New int `json:"new"`
}

const (
	NOTIFY        = "notify"      // HASH false无效(用户已点击) true 有效(消息通知用户未点击)
	MATCH_COUNT   = "matchCount"  // HASH cur:当前数,new:新增数 用户点击时设置新增数为0
	LOVE_ME_COUNT = "loveMeCount" // HASH cur:当前数,new:新增数 用户点击时设置新增数为0
)

func NewNotifyService(ctx context.Context, svcCtx *svc.ServiceContext) *NotifyService {
	return &NotifyService{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// HASH 0无效(用户已点击) 1 有效(消息通知用户未点击)
// {love:0,mine:0,msg:0 }
// 通知
func (notifyService *NotifyService) SetNotify(userId int, msg string, read uint) error {
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	luaScript := `
		local field = "user:" .. KEYS[2]
		local ret = redis.call("HGET",KEYS[1],field)
		local content = { love = 0, mine = 0, msg = 0 }
		-- 先判断是否存在
		if ret then
			-- 存在则先获取
			content = cjson.decode(ret)	
		end
		content[KEYS[3]] = KEYS[4]
		content = cjson.encode(content)
		redis.call("HSET",KEYS[1],field,content)
	`
	var luaArgs []interface{}
	luaArgs = append(luaArgs, luaScript) // 第一个参数是 Lua 脚本
	luaArgs = append(luaArgs, 4)         // 第二个参数是长度
	luaArgs = append(luaArgs, NOTIFY, userId, msg, read)
	_, err := redis.Do("EVAL", luaArgs...)
	return err
}

// HASH cur:当前数,new:新增数 用户点击时设置新增数为0
// 设置互相心动
func (notifyService *NotifyService) SetLoveEach(userId int) error {
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	luaScript := `
		local field = "user:" .. KEYS[2]
		local ret = redis.call("HGET",KEYS[1],field)
		local content = { cur = 0, new = 0 }
		-- 先判断是否存在
		if ret then
			-- 存在则先获取
			content = cjson.decode(ret)	
		end
		content['cur'] = content['cur'] + 1
		content['new'] = content['new'] + 1
		content = cjson.encode(content)
		redis.call("HSET",KEYS[1],field,content)
	`
	var luaArgs []interface{}
	luaArgs = append(luaArgs, luaScript) // 第一个参数是 Lua 脚本
	luaArgs = append(luaArgs, 2)         // 第二个参数是长度
	luaArgs = append(luaArgs, MATCH_COUNT, userId)
	_, err := redis.Do("EVAL", luaArgs...)
	return err
}

// 用户已读互相心动
func (notifyService *NotifyService) ReadLoveEach(userId int) error {
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	luaScript := `
		local field = "user:" .. KEYS[2]
		local ret = redis.call("HGET",KEYS[1],field)
		local content = { cur = 0, new = 0 }
		-- 先判断是否存在
		if ret then
			-- 存在则先获取
			content = cjson.decode(ret)
			content['new'] = 0	
		end
		content = cjson.encode(content)
		redis.call("HSET",KEYS[1],field,content)
	`
	var luaArgs []interface{}
	luaArgs = append(luaArgs, luaScript) // 第一个参数是 Lua 脚本
	luaArgs = append(luaArgs, 2)         // 第二个参数是长度
	luaArgs = append(luaArgs, MATCH_COUNT, userId)
	_, err := redis.Do("EVAL", luaArgs...)
	return err
}

// 获取匹配数
func (notifyService *NotifyService) GetLoveEachCount(userId int) (*Love, error) {
	field := "user:" + strconv.Itoa(userId)
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	ret, err := redis.Do("HGET", MATCH_COUNT, field)
	if ret != nil {
		var love Love
		json.Unmarshal(ret.([]byte), &love)
		return &love, nil
	} else {
		return nil, err
	}
}

// 设置心动我的
func (notifyService *NotifyService) SetLoveMe(userId int) error {
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	luaScript := `
		local field = "user:" .. KEYS[2]
		local ret = redis.call("HGET",KEYS[1],field)
		local content = { cur = 0, new = 0 }
		-- 先判断是否存在
		if ret then
			-- 存在则先获取
			content = cjson.decode(ret)	
		end
		content['cur'] = content['cur'] + 1
		content['new'] = content['new'] + 1
		content = cjson.encode(content)
		redis.call("HSET",KEYS[1],field,content)
	`
	var luaArgs []interface{}
	luaArgs = append(luaArgs, luaScript) // 第一个参数是 Lua 脚本
	luaArgs = append(luaArgs, 2)         // 第二个参数是长度
	luaArgs = append(luaArgs, LOVE_ME_COUNT, userId)
	_, err := redis.Do("EVAL", luaArgs...)
	return err
}

// 用户已读心动我的
func (notifyService *NotifyService) ReadLoveMe(userId int) error {
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()

	luaScript := `
		local field = "user:" .. KEYS[2]
		local ret = redis.call("HGET",KEYS[1],field)
		local content = { cur = 0, new = 0 }
		-- 先判断是否存在
		if ret then
			-- 存在则先获取
			content = cjson.decode(ret)
			content['new'] = 0	
		end
		content = cjson.encode(content)
		redis.call("HSET",KEYS[1],field,content)
	`
	var luaArgs []interface{}
	luaArgs = append(luaArgs, luaScript) // 第一个参数是 Lua 脚本
	luaArgs = append(luaArgs, 2)         // 第二个参数是长度
	luaArgs = append(luaArgs, LOVE_ME_COUNT, userId)
	_, err := redis.Do("EVAL", luaArgs...)
	return err
}

// 获取心动我的数
func (notifyService *NotifyService) GetLoveMeCount(userId int) (*Love, error) {
	field := "user:" + strconv.Itoa(userId)
	redis := notifyService.svcCtx.RedisPool.Get()
	defer redis.Close()
	ret, err := redis.Do("HGET", LOVE_ME_COUNT, field)
	if ret != nil {
		var love Love
		json.Unmarshal(ret.([]byte), &love)
		return &love, nil
	} else {
		return nil, err
	}
}
